#include "HUDlife.h"
#include "Transformation.h"
#include "TextureManager.h"
#include "GLTexture2D.h"

using namespace game_engine;
using namespace game_engine::library;
using namespace game_engine::components;
using namespace game_engine::open_gl;
using namespace game_engine::utils;

///////////////////////////////////////////////////////////////////////////////
// Constructors and Destructors
///////////////////////////////////////////////////////////////////////////////

HUDlife::HUDlife(void)
    : GameObject(), maxLifes(0), textureFile(), textureType(), position()
{
    lifes.clear();
}

HUDlife::HUDlife(Scene *scn, const string &n, const string &t, 
    const DrawLevelType d)
    : GameObject(scn, n, t, d), maxLifes(0), textureFile(), textureType(), 
    position()
{
    lifes.clear();
}

HUDlife::~HUDlife(void)
{
}

///////////////////////////////////////////////////////////////////////////////
// Other public methods
///////////////////////////////////////////////////////////////////////////////

void HUDlife::initialize(void)
{
    // Create the Transformation component and add it to the object
    Transformation *transf = new Transformation(this, position.getX(), 
        position.getY(), 0.0, 0.0, 1.0, 1.0);   
    addComponent(transf);

    // Create the PlainTexture components and add them to the object
    for (UINT32 i=0; i<maxLifes; i++)
    {
        lifes.push_back(new PlainTexture(this, textureFile, textureType, true));
        addComponent(lifes.at(i));
    }

    // Call initialization method from parent
    GameObject::initialize();
}

void HUDlife::render(void)
{
    // Retrieve the Transformation component
    Transformation *t = dynamic_cast<Transformation*> (getComponent("Transformation"));

    // Loop through all PlainTexture components and render them
    vector<PlainTexture*>::const_iterator it;
    for(it=lifes.begin(); it!=lifes.end(); it++)
    {
        (*it)->render();

        // Update the Transformation component in order to avoid overlap
        Point currentPos = t->getPosition();

        // Retrieve texture from TextureManager
        TextureManager *tm = TextureManager::getInstance();
        GLTexture2D texture = tm->getTexture(textureFile);
        t->setPosition(Point(currentPos.getX() + (texture.getPixelsWide()), 
            currentPos.getY()));
    }

    // Set the Transformation component to the initial value
    t->setPosition(position);
}

void HUDlife::decrementLifes(void)
{
    lifes.pop_back();
}

///////////////////////////////////////////////////////////////////////////////
// Getters
///////////////////////////////////////////////////////////////////////////////

const UINT32 HUDlife::getRemainingLifes(void) const
{
    return lifes.size();
}

///////////////////////////////////////////////////////////////////////////////
// Setters
///////////////////////////////////////////////////////////////////////////////

void HUDlife::setMaxLifes(const UINT32 num)
{
    maxLifes = num;
    //lifes.resize(num);
}

void HUDlife::setTextureFile(const string &file)
{
    textureFile = file;
}

void HUDlife::setTextureType(const INT32 type)
{
    textureType = type;
}

void HUDlife::setPosition(const utils::Point &pos)
{
    position = pos;

    // If a Transformation component already exists
    if (hasComponent("Transformation"))
    {
        // Retrieve the component and update its position
        Transformation *t = dynamic_cast<Transformation*> 
            (getComponent("Transformation"));
        t->setPosition(pos);
    }
}
